# compilation and packing of the native library for vorbis
#

SHELL = /bin/sh

prefix=@prefix@
JAVADIR=$(prefix)
REL_JAVAEXTLIBPATH=@REL_JAVAEXTLIBPATH@
JAVAEXTLIBPATH=$(JAVADIR)/$(REL_JAVAEXTLIBPATH)
OSNAME=@OSNAME@
ARCHNAME=@ARCHNAME@
CC=@CC@
CPPFLAGS=-g -Wall -I. -I$(JAVADIR)/include -I$(JAVADIR)/include/$(OSNAME) -fPIC -D_REENTRANT #-pedantic
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
LN_S=@LN_S@
RM_F=@RM_F@
MKDIR_P=@MKDIR_P@

OBJDIR=../../../@BUILD_DIR@
vpath %.class $(OBJDIR)/org/tritonus/lowlevel/pogg
vpath %.class $(OBJDIR)/org/tritonus/lowlevel/pvorbis

LIBBASENAME=libtritonuspvorbis
LIBDEST=$(LIBBASENAME).so.1.0
LIBDEST_ABSOLUTE=@SRC_ABSOLUTE_PATH@/src/lib/pvorbis/$(LIBDEST)
#or LIBDEST_ABSOLUTE=$(CURDIR)/$(LIBDEST)

LIBS=

JAVAH = @JAVAH@

VORBISOBJ=\
	analysis.o \
	bitrate.o \
	block.o \
	codebook.o \
	envelope.o \
	floor0.o \
	floor1.o \
	info.o \
	lpc.o \
	lsp.o \
	mapping0.o \
	mdct.o \
	psy.o \
	registry.o \
	res0.o \
	sharedbook.o \
	smallft.o \
	synthesis.o \
	vorbisenc.o \
	window.o

OGGOBJ=bitwise.o framing.o

JNIOBJ=\
	org_tritonus_lowlevel_pogg_Buffer.o \
	org_tritonus_lowlevel_pogg_Packet.o \
	org_tritonus_lowlevel_pvorbis_Block.o \
	org_tritonus_lowlevel_pvorbis_DspState.o \
	org_tritonus_lowlevel_pvorbis_Info.o

OBJ=\
	$(JNIOBJ) \
	../common/common.o \
	$(VORBISOBJ) \
	$(OGGOBJ)

JNIHEADERS:=$(JNIOBJ:.o=.h)

JNICLASSFILES=\
	Buffer.class \
	Packet.class \
	Block.class \
	DspState.class \
	Info.class

JNICLASSES=\
	org.tritonus.lowlevel.pogg.Buffer \
	org.tritonus.lowlevel.pogg.Packet \
	org.tritonus.lowlevel.pvorbis.Block \
	org.tritonus.lowlevel.pvorbis.DspState \
	org.tritonus.lowlevel.pvorbis.Info

#----------------- rules -------------------

.PHONY: all
all: $(LIBDEST)

$(LIBDEST):	$(OBJ)
	$(CC) -nostartfiles -shared -Wl,-soname,$(LIBBASENAME).so.1 -o $(LIBDEST) $(OBJ) -L/usr/lib $(LIBS)
	$(RM_F) $(LIBBASENAME).so.1
	$(LN_S) $(LIBBASENAME).so.1.0 $(LIBBASENAME).so.1
	$(RM_F) $(LIBBASENAME).so
	$(LN_S) $(LIBBASENAME).so.1 $(LIBBASENAME).so

$(OBJ): $(JNIHEADERS)

$(JNIHEADERS): $(JNICLASSFILES)
	$(RM_F) $(JNIHEADERS)
	$(JAVAH) -d . $(JNICLASSES)

.PHONY: install
install:
	@echo "Installing $(LIBBASENAME) in $(JAVAEXTLIBPATH)"
	@$(MKDIR_P) $(JAVAEXTLIBPATH)
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so.1.0
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so.1
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so
	$(INSTALL_PROGRAM) $(LIBDEST) $(JAVAEXTLIBPATH)
	@(cd $(JAVAEXTLIBPATH) && $(LN_S) $(LIBBASENAME).so.1.0 $(LIBBASENAME).so.1)
	@(cd $(JAVAEXTLIBPATH) && $(LN_S) $(LIBBASENAME).so.1 $(LIBBASENAME).so)

.PHONY: install-link
install-link:
	@echo "Creating links for $(LIBBASENAME) in $(JAVAEXTLIBPATH)"
	@$(MKDIR_P) $(JAVAEXTLIBPATH)
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so.1.0
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so.1
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so
	@(cd  $(JAVAEXTLIBPATH) && $(LN_S) $(LIBDEST_ABSOLUTE) $(LIBBASENAME).so.1.0)
	@(cd $(JAVAEXTLIBPATH) && $(LN_S) $(LIBBASENAME).so.1.0 $(LIBBASENAME).so.1)
	@(cd $(JAVAEXTLIBPATH) && $(LN_S) $(LIBBASENAME).so.1 $(LIBBASENAME).so)

.PHONY: uninstall
uninstall:
	@echo "Removing $(LIBBASENAME) from $(JAVAEXTLIBPATH)"
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so.1.0
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so.1
	@$(RM_F) $(JAVAEXTLIBPATH)/$(LIBBASENAME).so

.PHONY: uninstall-link
uninstall-link: uninstall

.PHONY: clean
clean:
	rm -rf $(LIBDEST) $(JNIHEADERS) *.o *~
